import { http, method } from "@/utils/http";
import { type TreeData } from "./Struct";

/** 接口数据结构 */
interface Invoke {
    success?: Function
    fail?: Function
    body?: FormData
}

/** 表单链式编程 */
class GetFormData {
    private data = new FormData()
    static getData() {
        return new GetFormData()
    }
    set(name: string, value: string | Blob) {
        this.data.append(name, value)
        return this;
    }
    toFormData() {
        return this.data;
    }
}

/** 分页数据结构 */
interface Page extends Invoke {
    page_index: number
    page_size?: number
}

/** 用户登录注销 */
class LoginApi {
    /** 退出登录 */
    logout(invoke: Invoke) {
        http.request("logout", { method: method.GET, }, (data: any) => {
            invoke.success?.(data);
        },
            (error: any) => {
                invoke.fail?.(error)
            }
        )
    }
    /**登录 */
    login(invoke: Invoke) {
        http.request("login", {
            method: method.POST,
            body: invoke.body,
        },
            (data: any) => {
                invoke.success?.(data);
            },
            (err: any) => {
                invoke.fail?.(err);
            }
        )
    }
    /** 获取认证码 */
    flushAuthenticationCode(invoke: Invoke) {
        http.getImage("get_img/", (data: Blob) => {
            const objectUrl = URL.createObjectURL(data);
            if (invoke.success != null) {
                invoke.success(objectUrl);
            }
        })
    }
}

/** 组织api */
class ORGApi {
    getOrg(invoke: Invoke) {
        http.request("get_org/", { method: method.GET, body: invoke.body },
            (data: TreeData[]) => {
                let treeMap: Map<number, TreeData> = new Map()
                data.forEach((element: TreeData) => {
                    element.index = { id: element.id, pid: element.pid, label: element.name, children: [] }
                    element.index.index = element;
                    treeMap.set(element.id, element)
                });
                data.forEach((element: TreeData) => {
                    if (element.pid != null && treeMap.has(element.pid)) {
                        let node = treeMap.get(element.pid)
                        node?.index.children.push(element.index)
                    }
                });
                invoke.success?.(treeMap)
            }, (err: any) => {
                invoke.fail?.(err)
            }
        )
    }
}

/** 首页api */
class HomeApi {
    getLogins(page: Page) {
        http.request("logins/?page_index=" + page.page_index + "&page_size=" + (page.page_size == null ? 10 : page.page_size),
            { method: method.GET },
            (data: any) => { page.success?.(data) },
            (err: any) => { page.fail?.() }
        )
    }
}

/** 虚拟机api */
class VmboxApi {
    getHosts(invoke: Invoke) {
        http.request("virt/domain/hosts/", {
            method: method.GET,
        }, invoke.success, invoke.fail)
    }
    addDir(invoke: Invoke) {
        http.request('virt/domain/add_virt_dirs/', {
            method: method.POST,
            body: invoke.body
        },
            invoke.success,
            invoke.fail
        )
    }
    delDir(invoke: Invoke) {
        http.request("virt/domain/del_virt_dirs/?id=" + invoke.body?.get("id"), {
            method: method.DELETE
        },
            invoke.success,
            invoke.fail
        )
    }
    getXml(invoke: Invoke) {
        http.request("virt/domain/xml/",
            { method: method.POST, body: invoke.body },
            invoke.success, invoke.fail)
    }
    moveHost(invoke: Invoke) {
        http.request("virt/domain/move_host/", {
            method: method.POST,
            body: invoke.body
        }, invoke.success, invoke.fail)
    }
    start(invoke: Invoke) {
        http.request("virt/domain/start/", {
            body: invoke.body,
            method: method.POST
        },
            invoke?.success,
            invoke.fail
        )
    }
    shutdown(invoke: Invoke) {
        http.request("virt/domain/shutdown/", {
            body: invoke.body,
            method: method.POST
        },
            invoke?.success,
            invoke.fail
        )
    }
    destroy(invoke: Invoke) {
        http.request("virt/domain/destroy/", {
            body: invoke.body,
            method: method.POST
        },
            invoke?.success,
            invoke.fail
        )
    }
}

export const getBody = GetFormData.getData;
export const vmboxApi = new VmboxApi();
export const homeApi = new HomeApi();
export const loginApi = new LoginApi();
export const orgApi = new ORGApi();

